// pages/search/search.ts
var { request } = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    assignList: [] as any,
    role :wx.getStorageSync('role'),
    params:{
      "assigneeName": "",
      "name": "",
      "current":"1",
      "size":"100"
    },
    list: [] as any,
    progress:'',
    comment:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {
    this.initAssignList()
    this.handleSearch()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },
  handleSearch(){
    request('/admin/task/list', {
      method: 'POST',
      data:this.data.params
    }).then((res: any) => {
      if (res.data.code == 1) {
        let list = res.data.data.records.map(item=>{
          item.isOpen=false
          item.isUrgent = item.level==1
          return item
        })
        this.setData({
          list: list.filter((item: any) => item.status == 0)
        })
        console.log(this.data.list, 'list');
      }
    })
  },
  changName(e){
    console.log(e.detail.value,'changName');
    this.setData({
      'params.name':e.detail.value
    })
  },
  async handleDrawer(e: any) {
    console.log(e.currentTarget.dataset);
    let params = e.currentTarget.dataset
    this.data.list[params.index].isOpen = !this.data.list[params.index].isOpen
    console.log(this.data.list, 'handleDrawer');
    this.setData({
      list: this.data.list,
      // 'list[params.index].isOpen':!this.data.list[params.index].isOpen
    })
  },
  handleEdit(e: any) {
    let param = e.currentTarget.dataset
    console.log(param);
    wx.navigateTo({
      url: '/pages/edit/edit?addFlag=0&&todo='+JSON.stringify(param.todo),
    })
  },
  handleDel(e){
    let param = e.currentTarget.dataset
    console.log(param);
    let that = this
    wx.showModal({
      content: '确认删除当前待办任务吗？',
      success (res) {
        if (res.confirm) {
          console.log('用户点击确定')
          request('/admin/task/delete', {
            method:'POST',
            data:{
              id:param.id
            }
          }).then((res: any) => {
            if (res.data.code == 1) {
              let list = res.data.data.records
              that.setData({
                assignList: list.filter((item: any) => item.processStatus == 1),
              })
              console.log(that.data.assignList, 'assignList');
            }
          })
          that.handleSearch()
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  assignChange: function (e) {
    console.log(e.detail.value,'assignChange')
    this.setData({
      'params.assignee':e.detail.value,
      'params.assigneeName':this.data.assignList[e.detail.value].name
    })
    this.handleSearch()
  },
  initAssignList() {
    request('/admin/employee/page?page=1&pageSize=100', {
    }).then((res: any) => {
      if (res.data.code == 1) {
        let list = res.data.data.records
        this.setData({
          assignList: list.filter((item: any) => 
          item.processStatus == 1&&item.status==1),
        })
        console.log(this.data.assignList, 'assignList');
      }
    })
  },
  handleBack(){
    wx.navigateBack({
      delta: 2
    })
  },
  bindProgressInput(e: any){
    console.log(e.detail.value,'bindProgressBlur');
    this.setData({
      progress:e.detail.value
    })
  },
  bindCommentInput(e: any){
    console.log(e.detail.value,'bindCommentBlur');
    this.setData({
      comment:e.detail.value
    })
  },
  handleProgress(e: any){
    let param = e.currentTarget.dataset
    console.log(param,'handleProgress');
    request('/admin/task', {
      method:'POST',
      data:{
        id:param.id,
        progress:this.data.progress
      }
    }).then((res: any) => {
      if (res.data.code == 1) {
        console.log(res.data,'res.data');
      }
    })
  },
  handleComment(e: any){
    let param = e.currentTarget.dataset
    console.log(param,'handleComment');
    request('/admin/task', {
      method:'POST',
      data:{
        id:param.id,
        comment:this.data.comment
      }
    }).then((res: any) => {
      if (res.data.code == 1) {
        console.log(res.data,'res.data');
      }
    })
  },
  handleUnfinish(e: any){
    let param = e.currentTarget.dataset
    wx.navigateTo({
      url: '/pages/unfinish/unfinish?id='+param.id,
    })
  },
  handleFinish(e: any){
    let param = e.currentTarget.dataset
    let that = this
    console.log(param,'handleFinish');
    wx.showModal({
      content: '确认完成当前待办任务吗？',
      success (res) {
        if (res.confirm) {
          request('/admin/task', {
            method:'POST',
            data:{
              id:param.id,
              status:1
            }
          }).then((res: any) => {
            if (res.data.code == 1) {
              console.log(res.data,'res.data');
              // that.initTodoList()
              that.handleSearch()

            }
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})